<template>
    <div class="app-container">
        <el-card class="operate-container" shadow="never">
            <i class="el-icon-tickets"></i>
            <span>数据列表</span>
            <el-button size="mini" class="btn-add" @click="handleAdd()">添加设备</el-button>
        </el-card>
        <div class="table-container">
            <el-table
                    ref="deviceTable"
                    :data="list"
                    style="width: 100%;"
                    @selection-change="handleSelectionChange"
                    v-loading="listLoading"
                    border
            >
                <el-table-column type="selection" width="60" align="center"></el-table-column>
                <el-table-column label="编号" width="120" align="center">
                    <template slot-scope="scope">{{scope.row.id}}</template>
                </el-table-column>
                <el-table-column label="设备ID" width="250" align="center">
                    <template slot-scope="scope">{{scope.row.deviceId}}</template>
                </el-table-column>
                <el-table-column label="mac地址" width="250" align="center">
                    <template slot-scope="scope">{{scope.row.macAddr}}</template>
                </el-table-column>
                <el-table-column label="serialno" width="250" align="center">
                    <template slot-scope="scope">{{scope.row.serialno}}</template>
                </el-table-column>
                <el-table-column label="操作" width="240" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="handleUpdate(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="pagination-container">
            <el-pagination
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    layout="total, sizes,prev, pager, next,jumper"
                    :page-size="listQuery.pageSize"
                    :page-sizes="[5,10,15]"
                    :current-page.sync="listQuery.pageNum"
                    :total="total"
            ></el-pagination>
        </div>
    </div>
</template>
<script>
    import {fetchList, deleteGatewayinfo} from "@/api/qsg/gatewayinfo";
    import {formatDate} from "@/utils/date";

    const defaultListQuery = {
        pageNum: 1,
        pageSize: 10
    };
    export default {
        name: "deviceList",
        data() {
            return {
                listQuery: Object.assign({}, defaultListQuery),
                list: null,
                total: null,
                listLoading: false,
                multipleSelection: []
            };
        },
        created() {
            this.getList();
        },
        filters: {
            formatTime(time) {
                if (time == null || time === "") {
                    return "N/A";
                }
                var n = time.replace("-", "/");
                n = n.replace("-", "/");
                let date = new Date(n);
                return formatDate(date, "yyyy-MM-dd hh:mm:ss");
            },
            statusChange(status) {
                if (status == 4) {
                    return '是'
                } else {
                    return '否'
                }
            }
        },
        methods: {
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.listQuery.pageNum = val;
                this.getList();
            },
            handleDelete(index, row) {
                this.$confirm("是否要删除该条记录", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    deleteGatewayinfo(row.id).then(response => {
                        this.$message({
                            message: "删除成功",
                            type: "success",
                            duration: 1000
                        });
                        this.getList();
                    });
                });
            },
            handleAdd() {
                this.$router.push({path: "/gateway/addGatewayinfo"});
            },
            handleUpdate(index, row) {
                this.$router.push({
                    path: "/gateway/updateGatewayinfo",
                    query: {id: row.id}
                });
            },
            getList() {
                this.listLoading = true;
                fetchList(this.listQuery).then(response => {
                    this.listLoading = false;
                    this.list = response.data.records;
                    this.total = response.data.total;
                });
            }
        }
    };
</script>
<style scoped>
    .input-width {
        width: 203px;
    }
</style>


